package com.blb.trans.service.impl;

import com.blb.trans.entity.Account;
import com.blb.trans.mapper.AccountMapper;
import com.blb.trans.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    @Transactional(isolation = Isolation.REPEATABLE_READ)
    @Override
    public void transfer(Long fromId, Long toId, Long money) {
        Account fromAccount = accountMapper.selectById(fromId);
        if(fromAccount.getAccountBalance() < money){
            throw new RuntimeException("金额不足");
        }
        //从转出账户扣除金额
        accountMapper.updateBalanceById(fromId,fromAccount.getAccountBalance() - money);
//        int x = 10 / 0;
        //在转入账户添加金额
        Account toAccount = accountMapper.selectById(toId);
        accountMapper.updateBalanceById(toId,toAccount.getAccountBalance() + money);
        System.out.println("完成转账业务");
    }
}
